package com.ywyself.plugin.spider.uils;

/**
 * 发送请求
 */
public class SendHttp {
    private static String[] userAgent = new String[]{
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0" // firefox 57.0.4
            , "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36" // Google Chrome 63.0.3239.132
            , "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4295.400 QQBrowser/9.7.12777.400" // qq浏览器9.7（12777）
            , "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36" // 360浏览器
            , "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;" // IE 9.0
            , "Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11" // Opera 11.11 – MAC
            , "Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11" // Opera 11.11 – Windows
    };

    // 单例模式 https://juejin.im/post/5a643629f265da3e5b32e59c
    private static java.util.Random getRandom() {
        return RandomHolder.instance;
    }

    private static class RandomHolder {
        private static java.util.Random instance = new java.util.Random();
    }

    /**
     * 发送get请求
     *
     * @param url
     * @param charset
     * @return
     */
    public static String get(String url, String charset) {
        if (url == null || url.trim().length() == 0) {
            System.out.println("请求地址不能为空");
            return null;
        }
        // 创建httpClient连接
        org.apache.http.impl.client.CloseableHttpClient client = org.apache.http.impl.client.HttpClients.createDefault();
        // 创建一个HttpGet请求
        org.apache.http.client.methods.HttpGet request = new org.apache.http.client.methods.HttpGet(url);
        // 设置header
        request.setHeader("User-Agent", userAgent[getRandom().nextInt(userAgent.length)]);
        // 获取响应信息
        org.apache.http.client.methods.CloseableHttpResponse response = null;
        try {
            response = client.execute(request);
            // 得到httpResponse的状态响应码
            if (response.getStatusLine().getStatusCode() == org.apache.http.HttpStatus.SC_OK) {
                // 处理http返回码200的情况
                // 得到httpResponse的实体数据
                org.apache.http.HttpEntity httpEntity = response.getEntity();
                if (httpEntity == null) {
                    System.out.println("没有获取的响应实体数据");
                    return null;
                } else {
                    return org.apache.http.util.EntityUtils.toString(httpEntity, (charset == null || charset.trim().length() == 0) ? "UTF-8" : charset);
                }
            } else {
                System.out.println("http get请求出错");
                System.out.println("响应码：" + response.getStatusLine().getStatusCode());
                return null;
            }
        } catch (java.io.IOException e) {
            System.out.println("http get请求出错");
            System.out.println(e.getLocalizedMessage());
            return null;
        } finally {
            // 关闭连接,释放资源
            try {
                if (response != null) {
                    response.close();
                }
                client.close();
            } catch (java.io.IOException e) {
                e.printStackTrace();
            }
        }
    }
}
